<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/11/5
 * Time: 10:05
 */

namespace Professor\Controller;

use Common\Controller\AdminbaseController;
use Professor\Model\ProfessorModel;

class AdminProfessorController extends AdminbaseController {

    protected $pf_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size    = 20;
        $this->pf_model     = D("Professor/Professor");
    }

    // 专栏作者列表
    public function professor_list()
    {
        $this->_lists();
        $this->display();
    }

    // 审核专栏作者
    public function operate_pf()
    {
        $id     = I('get.id', 0, "intval");
        $type   = I('get.type', 'del', 'strval');

        $pf_info = $this->pf_model->where(['id' => $id])->find();
        if ($pf_info) {
            if ($type == 'add') {
                $data['del_time']   = 0;
                $data['status']     = ProfessorModel::PF_STATUS_NORMAL;
            } else {
                $data['del_time']   = time();
                $data['status']     = ProfessorModel::PF_STATUS_FIBIDDEN;
            }
            $this->pf_model->where(['id' => $id])->save($data);

            $this->success('操作成功');
        } else {
            $this->error('数据有误');
        }
    }

    // 专栏作者信息
    public function pf_info()
    {
        $id         = I('get.id', 0, "intval");
        $pf_info    = $this->pf_model
            ->alias('pf')
            ->where(['pf.id' => $id])
            ->field('pf.*, ( SELECT GROUP_CONCAT(name) FROM rx_label WHERE FIND_IN_SET(id, pf.labels) ) as label_names')
            ->find();

        if (!empty($pf_info['label_names'])) {
            $pf_info['label_names'] = explode(',', $pf_info['label_names']);
        }

        if ($pf_info) {
            $user_info = M('users')->where(['id' => $pf_info['user_id']])->find();
            if ($user_info) {

                $this->assign('user_info', $user_info);
                $this->assign('pf_info', $pf_info);

                $where = ['pf_id' => $id, 'del_time' => 0, 'user_id' => $pf_info['user_id']];

                // 项目
                $pf_projects = M('pf_project')->where($where)->select();
                $this->assign('pf_projects', $pf_projects);

                // 教育
                $pf_education = M('pf_education')->where($where)->select();
                $this->assign('pf_education', $pf_education);

                // 出版作品
                $pf_publish = M('pf_publish')->where($where)->select();
                $this->assign('pf_publish', $pf_publish);

                // 资格认证
                $pf_certificate = M('pf_certificate')->where($where)->select();
                $this->assign('pf_certificate', $pf_certificate);

                // 专利发明
                $pf_patent = M('pf_patent')->where($where)->select();
                $this->assign('pf_patent', $pf_patent);

                // 荣誉奖项
                $pf_award = M('pf_award')->where($where)->select();
                $this->assign('pf_award', $pf_award);

                $this->display();
            } else {
                $this->error('数据有误');
            }
        } else {
            $this->error('数据有误');
        }
    }

    public function update_pf_info()
    {
        $pf_info = I('post.pf_info');
        if(isset($pf_info['id'])) {

            $pf_info['fens_qrcode'] = sp_asset_relative_url($pf_info['fens_qrcode']);
            $this->pf_model->where(['id' => $pf_info['id']])->save($pf_info);
        }
        $this->success('成功');
    }


    /**
     * @param array $where 查询条件
     */
    private function _lists($where = array()){

        if ($name = I("post.name")) {
            $where['u.user_nicename'] = array('like',"%$name%");
            $this->assign("name", $name);
        }

        $count  = $this->pf_model->alias("p")->join("__USERS__ u ON p.user_id = u.id")->where($where)->count();
        $page   = $this->page($count, $this->page_size);

        $this->pf_model
            ->alias("p")
            ->join("__USERS__ u ON p.user_id = u.id")
            ->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->field("u.*, p.id as pf_id, p.created_at, p.updated_at, p.status, p.pf_order")
            ->order("p.pf_order desc, p.status asc");

        $pf_lists = $this->pf_model->select();

        foreach ($pf_lists as $k => $pf) {
            $pf['created_at'] = date("Y-m-d H:i:s", $pf['created_at']);
            $pf['updated_at'] = date("Y-m-d H:i:s", $pf['updated_at']);
            $pf_lists[$k] = $pf;
        }

        $this->assign("page", $page->show('Admin'));
        $this->assign("pf_list", $pf_lists);
    }

    // 动态调整post排序
    public function change_pf_order()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            $this->pf_model->where(['id' => $id])->save(['pf_order' => $order]);
            $this->success('修改成功');
        }
    }


}